package com.suneast.suneaststore.ui.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
import android.view.View;

import com.suneast.suneaststore.R;

/**
 * Created by zestz on 2018/3/30.
 */

public class ZzCheckBox extends AppCompatButton implements View.OnClickListener {

    private boolean isChecked;
    private final Drawable checkedDrawable;
    private final Drawable unCheckedDrawable;
    private final Integer checkedTextColor;
    private final Integer unCheckedTextColor;
    private final Integer unAbleTextColor;

    public ZzCheckBox(Context context) {
        this(context, null);
    }

    public ZzCheckBox(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZzCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.ZzCheckBox, defStyleAttr, defStyleAttr);
        checkedDrawable = a.getDrawable(R.styleable.ZzCheckBox_checked_drawable);
        unCheckedDrawable = a.getDrawable(R.styleable.ZzCheckBox_unchecked_drawable);
        checkedTextColor = a.getColor(R.styleable.ZzCheckBox_checked_text_color, 0);
        unCheckedTextColor = a.getColor(R.styleable.ZzCheckBox_unchecked_text_color, 0);
        isChecked = a.getBoolean(R.styleable.ZzCheckBox_checked, false);
        unAbleTextColor = a.getColor(R.styleable.ZzCheckBox_unable_text_color, 0);
        a.recycle();
        setCheckedBg(isChecked);
        setOnClickListener(this);
    }

    private void setCheckedBg(boolean isChecked) {
        if (isChecked) {
            if (checkedDrawable != null) {
                setBackground(checkedDrawable);
            }
        } else {
            if (unCheckedDrawable != null) {
                setBackground(unCheckedDrawable);
            }
        }
    }

    private void setCheckedText(boolean isChecked) {
        if (isChecked) {
            if (checkedTextColor != 0) {
                setTextColor(checkedTextColor);
            } else {
                setTextColor(Color.BLACK);
            }
        } else {
            if (unCheckedTextColor != 0) {
                setTextColor(unCheckedTextColor);
            } else {
                setTextColor(Color.BLACK);
            }
        }
    }

    public boolean isChecked() {
        return this.isChecked;
    }

    public void setChecked(boolean isChecked) {
        if (isChecked != this.isChecked) {
            this.isChecked = isChecked;
            setCheckedBg(this.isChecked);
            setCheckedText(this.isChecked);
        }
    }

    @Override
    public void onClick(View v) {
        isChecked = !isChecked;
        setCheckedBg(isChecked);
        setCheckedText(isChecked);
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged(v, isChecked);
        }
    }

    private OnCheckedChangeListener mOnCheckedChangeListener;

    public interface OnCheckedChangeListener {
        void onCheckedChanged(View v, boolean isChecked);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (unAbleTextColor != 0) {
            if (enabled) {
                setCheckedText(isChecked);
            } else {
                setTextColor(unAbleTextColor);
            }
        }
    }
}
